//
//  FrontPageHeaderView.swift
//  MM2
//
//  Created by 李成明 on 2021/12/31.
//

import UIKit

private typealias clickClosure = ((FrontPageHeaderView) -> Void)

class FrontPageHeaderView: UIView {
    
    private lazy var discoverLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont(name: "Helvetica-Bold", size: 20)
        view.textAlignment = .center
        view.textColor = mainColor
        view.text = "关注"
        
        view.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(discoverClickAction))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var followLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont(name: "Helvetica-Bold", size: 20)
        view.textAlignment = .center
        view.text = "发现"
        
        view.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(followClickAction))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var sliderView: UIView = {
        let view = UIView()
        view.backgroundColor = .initWithRGB(230, 100, 60)
        return view
    }()
    
    private lazy var addView: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "front_page_post_icon")
        
        view.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(addClickAction))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var searchView: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "front_page_search_icon")
        
        view.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(searchCickAction))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var bottomLine: UIView = {
        let view = UIView()
        view.backgroundColor = mainColor
        return view
    }()
    
    private lazy var currentIsFollow = true {
        willSet {
            if currentIsFollow != newValue {
                if newValue {
                    followLabel.textColor = .black
                    discoverLabel.textColor = mainColor
                } else {
                    followLabel.textColor = mainColor
                    discoverLabel.textColor = .black
                }
            }
        }
    }
    var scheduleValue: CGFloat = 0 {
        didSet {
            if scheduleValue <= 0.5 { currentIsFollow = true }
            else { currentIsFollow = false }
            
            let sliderLength = (0.5 - abs(scheduleValue - 0.5))*(kScreenWidth - 120) + 40
            let sliderLeft = (scheduleValue < 0.5) ? kScreenWidth/4 : (kScreenWidth/4*3 - sliderLength)
            sliderView.frame = CGRect(x: sliderLeft, y: viewHeight - 2, width: sliderLength, height: 4)
        }
    }
    
    private var viewHeight: CGFloat {
        return frame.size.height
    }
    
    private var viewWidth: CGFloat {
        return frame.size.width
    }
    
    
    private var addClickClosure: clickClosure?
    private var searchClickClosure: clickClosure?
    private var discoverClickClosure: clickClosure?
    private var followClickClosure: clickClosure?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.clipsToBounds = false
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var selectClosure: ((Int) -> Void)?
}

extension FrontPageHeaderView {
    private func setupUI() {
        addsubViews([addView, searchView, discoverLabel, followLabel, sliderView])
        addView.frame = CGRect(x: 16, y: 10, width: 24, height: 24)
        searchView.frame = CGRect(x: viewWidth - 16 - 24, y: 10, width: 24, height: 24)
        followLabel.frame = CGRect(x: 16 + 24, y: 0, width: kScreenWidth/2 - 40, height: viewHeight)
        discoverLabel.frame = CGRect(x: kScreenWidth/2, y: 0, width: kScreenWidth/2 - 40, height: viewHeight)
        bottomLine.frame = CGRect(x: 0, y: viewHeight - 1, width: kScreenWidth, height: 1)
        sliderView.frame = CGRect(x: kScreenWidth/4, y: viewHeight - 2, width: 40, height: 4)
        
    }
}

extension FrontPageHeaderView {

    @discardableResult
    func add(_ closure: @escaping ((FrontPageHeaderView) -> Void)) -> FrontPageHeaderView {
        addClickClosure = closure
        return self
    }

    @discardableResult
    func search(_ closure: @escaping ((FrontPageHeaderView) -> Void)) -> FrontPageHeaderView {
        searchClickClosure = closure
        return self
    }

    @discardableResult
    func discover(_ closure: @escaping ((FrontPageHeaderView) -> Void)) -> FrontPageHeaderView {
        discoverClickClosure = closure
        return self
    }

    @discardableResult
    func follow(_ closure: @escaping ((FrontPageHeaderView) -> Void)) -> FrontPageHeaderView {
        followClickClosure = closure
        return self
    }
    
    @objc private func addClickAction() {
        addClickClosure?(self)
    }
    
    @objc private func searchCickAction() {
        searchClickClosure?(self)
    }
    
    @objc private func followClickAction() {
        followClickClosure?(self)
    }
    
    @objc private func discoverClickAction() {
        discoverClickClosure?(self)
    }
}
